<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/09/04
 * Time: 14:58
 */

namespace business\elasticsearch\repositories;

use business\elasticsearch\models\LandConstructionModel;
use business\elasticsearch\models\LandProjectModel;
use business\elasticsearch\models\LandTakeModel;
use business\elasticsearch\types\LandConstructionMapConfigType;
use business\elasticsearch\types\LandProjectMapConfigType;
use business\elasticsearch\types\LandTakeMapConfigType;
use yii\elasticsearch\Query;
use business\elasticsearch\repositories\EsQuery;

class LandRepository
{
    /**
     * 创建拿地数据索引
     * @return mixed
     */
    public function createLandTakeIndex()
    {
        return LandTakeModel::getDb()
            ->createCommand()
            ->createIndex(
            LandTakeModel::index(),
            [
                'settings' => [
                    'index' => [
                        'number_of_shards' => 5,
                        'number_of_replicas' => 1,
                        'analysis.analyzer.default.type' => 'ik_max_word',
                    ]
                ],
                'mappings' => [
                    LandTakeModel::type() => LandTakeMapConfigType::mapConfig()
                ]
            ]
        );
    }

    /**
     * 创建开工数据索引
     * @return mixed
     */
    public function createLandConstructionIndex()
    {
        return LandConstructionModel::getDb()
            ->createCommand()
            ->createIndex(
            LandConstructionModel::index(),
            [
                'settings' => [
                    'index' => [
                        'number_of_shards' => 5,
                        'number_of_replicas' => 1,
                        'analysis.analyzer.default.type' => 'ik_max_word',
                    ]
                ],
                'mappings' => [
                    LandConstructionModel::type() => LandConstructionMapConfigType::mapConfig()
                ]
            ]
        );
    }

    /**
     * 创建项目数据索引
     * @return mixed
     */
    public function createLandProjectIndex()
    {
        return LandProjectModel::getDb()
            ->createCommand()
            ->createIndex(
            LandProjectModel::index(),
            [
                'settings' => [
                    'index' => [
                        'number_of_shards' => 5,
                        'number_of_replicas' => 1,
                        'analysis.analyzer.default.type' => 'ik_max_word',
                    ]
                ],
                'mappings' => [
                    LandProjectModel::type() => LandProjectMapConfigType::mapConfig()
                ]
            ]
        );
    }

    /**
     * 删除拿地数据索引
     * @return mixed
     */
    public function deleteLandTakeIndex()
    {
        return LandTakeModel::getDb()
            ->createCommand()
            ->deleteIndex(LandTakeModel::index(), LandTakeModel::type());
    }

    /**
     * 删除开工数据索引
     * @return mixed
     */
    public function deleteLandConstructionIndex()
    {
        return LandConstructionModel::getDb()
            ->createCommand()
            ->deleteIndex(LandConstructionModel::index(), LandConstructionModel::type());
    }

    /**
     * 删除项目数据索引
     * @return mixed
     */
    public function deleteLandProjectIndex()
    {
        return LandProjectModel::getDb()
            ->createCommand()
            ->deleteIndex(LandProjectModel::index(), LandProjectModel::type());
    }

    /**
     * 查询拿地数据单条记录
     * @param array $condition
     * @return array|bool
     */
    public function queryLandTakeOne($condition = [])
    {
        return (new Query())
            ->from(LandTakeModel::index(), LandTakeModel::type())
            ->where($condition)
            ->one();
    }

    /**
     * 查询开工数据单条记录
     * @param array $condition
     * @return array|bool
     */
    public function queryLandConstructionOne($condition = [])
    {
        return (new Query())
            ->from(LandConstructionModel::index(), LandConstructionModel::type())
            ->where($condition)
            ->one();
    }

    /**
     * 查询项目数据单条记录
     * @param array $condition
     * @return array|bool
     */
    public function queryLandProjectOne($condition = [])
    {
        return (new Query())
            ->from(LandProjectModel::index(), LandProjectModel::type())
            ->where($condition)
            ->one();
    }

    /**
     * 查询拿地数据记录
     * @param array $condition
     * @param array $include
     * @param array $aggs
     * @param int $from
     * @param int $size
     * @param array $orderBy
     * @return array
     */
    public function queryLandTakeList(
        $condition = [],
        $include = [],
        $aggregations = [],
        $from = 0,
        $size = 0,
        $orderBy = ['take_land_date' => SORT_DESC]
    ) {
        $query = (new EsQuery())
            ->source($include)
            ->from(LandTakeModel::index(), LandTakeModel::type())
            ->query($condition);

        if (!empty($aggregations)) {
            foreach ($aggregations as $aggregation) {
                $query->addAggs($aggregation['name'], $aggregation['options']);
            }
        }

        $query->orderBy($orderBy)->offset($from)->limit($size);

        return [
            $query->count(),
            !empty($aggregations) ? $query->createCommand()->search()['aggregations'] : $query->all()
        ];
    }

    /**
     * 查询开工数据记录
     * @param $condition
     * @param array $include
     * @param array $aggregations
     * @param int $from
     * @param int $size
     * @param array $orderBy
     * @return array
     */
    public function queryLandConstructionList(
        $condition,
        $include = [],
        $aggregations = [],
        $from = 0,
        $size = 0,
        $orderBy = ['issuing_time' => SORT_DESC]
    ) {
        $query = (new EsQuery())
            ->source($include)
            ->from(LandConstructionModel::index(), LandConstructionModel::type())
            ->query($condition);

        if (!empty($aggregations)) {
            foreach ($aggregations as $aggregation) {
                $query->addAggs($aggregation['name'], $aggregation['options']);
            }
        }

        $query->orderBy($orderBy)->offset($from)->limit($size);

        return [
            $query->count(),
            !empty($aggregations) ? $query->createCommand()->search()['aggregations'] : $query->all()
        ];
    }

    /**
     * 查询项目数据记录
     * @param $condition
     * @param int $from
     * @param int $size
     * @return array
     */
    public function queryLandProjectList(
        $condition,
        $include = [],
        $aggregations = [],
        $from = 0,
        $size = 0,
        $orderBy = ['created_time' => SORT_DESC]
    ) {
        $query = (new EsQuery())
            ->source($include)
            ->from(LandProjectModel::index(), LandProjectModel::type())
            ->query($condition);

        if (!empty($aggregations)) {
            foreach ($aggregations as $aggregation) {
                $query->addAggs($aggregation['name'], $aggregation['options']);
            }
        }

        $query->orderBy($orderBy)->offset($from)->limit($size);

        return [
            $query->count(),
            !empty($aggregations) ? $query->createCommand()->search()['aggregations'] : $query->all()
        ];
    }

    /**
     * 保存拿地数据记录
     * @param array $data
     * @return array|mixed
     */
    public function saveLandTakeRecord($data = [], $id = 0)
    {
        if (!empty($data)) {
            return LandTakeModel::getDb()
                ->createCommand()
                ->insert(LandTakeModel::index(), LandTakeModel::type(), $data, $id);
        }

        return [];
    }

    /**
     * 保存开工数据记录
     * @param array $data
     * @param int $id
     * @return array|mixed
     */
    public function saveLandConstructionRecord($data = [], $id = 0)
    {
        if (!empty($data)) {
            return LandConstructionModel::getDb()
                ->createCommand()
                ->insert(LandConstructionModel::index(), LandConstructionModel::type(), $data, $id);
        }

        return [];
    }

    /**
     * 保存项目数据记录
     * @param array $data
     * @param int $id
     * @return array|mixed
     */
    public function saveLandProjectRecord($data = [], $id = 0)
    {
        if (!empty($data)) {
            return LandProjectModel::getDb()
                ->createCommand()
                ->insert(LandProjectModel::index(), LandProjectModel::type(), $data, $id);
        }

        return [];
    }

    /**
     * 删除拿地数据数据
     * @param $id
     * @return mixed
     */
    public function deleteLandTakeRecord($id)
    {
        return LandTakeModel::getDb()
            ->createCommand()
            ->delete(LandTakeModel::index(), LandTakeModel::type(), $id);
    }

    /**
     * 删除开工数据记录
     * @param $id
     * @return mixed
     */
    public function deleteLandConstructionRecord($id)
    {
        return LandConstructionModel::getDb()
            ->createCommand()
            ->delete(LandConstructionModel::index(), LandConstructionModel::type(), $id);
    }

    /**
     * 删除项目数据记录
     * @param $id
     * @return mixed
     */
    public function deleteLandProjectRecord($id)
    {
        return LandProjectModel::getDb()
            ->createCommand()
            ->delete(LandProjectModel::index(), LandProjectModel::type(), $id);
    }

    /**
     * 更新拿地数据记录
     * @param $id
     * @param array $data
     * @return mixed
     */
    public function updateLandTakeRecord($id, $data = [])
    {
        return LandTakeModel::getDb()
            ->createCommand()
            ->update(LandTakeModel::index(), LandTakeModel::type(), $id, $data);
    }

    /**
     * 更新开工数据记录
     * @param $id
     * @param array $data
     */
    public function updateLandConstructionRecord($id, $data = [])
    {
        return LandConstructionModel::getDb()
            ->createCommand()
            ->update(LandConstructionModel::index(), LandConstructionModel::type(), $id, $data);
    }

    /**
     * 更新项目数据记录
     * @param $id
     * @param array $data
     */
    public function updateLandProjectRecord($id, $data = [])
    {
        return LandProjectModel::getDb()
            ->createCommand()
            ->update(LandProjectModel::index(), LandProjectModel::type(), $id, $data);
    }
}
